package com.ruoyi.tea.api;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.tea.domain.TeaSearch;
import com.ruoyi.tea.domain.TeaUser;
import com.ruoyi.tea.service.ITeaSearchService;
import com.ruoyi.tea.service.ITeaUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 搜索API
 *
 * @author wangfeng
 * @time 2022年08月04日21:55:21
 */
@Controller
@RequestMapping("/wx/api/search")
public class SearchApiController {

    @Autowired
    private ITeaSearchService iTeaSearchService;

    @Autowired
    private ITeaUserService iTeaUserService;

    @RequestMapping("/history/save")
    @ResponseBody
    public AjaxResult historySave(@RequestBody TeaSearch teaSearch) {
        TeaUser tmp = new TeaUser();
        tmp.setOpenId(teaSearch.getOpenId());
        List<TeaUser> teaUserList = iTeaUserService.selectTeaUserList(tmp);
        if (teaUserList != null && teaUserList.size() != 0) {
            teaSearch.setUserCode(teaUserList.get(0).getUserCode());
            teaSearch.setPhone(teaUserList.get(0).getPhone());
            teaSearch.setUserName(teaUserList.get(0).getName());
            iTeaSearchService.insertTeaSearch(teaSearch);
            return AjaxResult.success();
        }
        return AjaxResult.error("未登录");
    }

    @RequestMapping("/history")
    @ResponseBody
    public AjaxResult history(@RequestBody TeaSearch teaSearch) {
        TeaUser tmp = new TeaUser();
        tmp.setOpenId(teaSearch.getOpenId());
        List<TeaUser> teaUserList = iTeaUserService.selectTeaUserList(tmp);
        if (teaUserList != null && teaUserList.size() != 0) {
            return AjaxResult.success(iTeaSearchService.selectTeaSearchHistoryList(teaSearch));
        }
        return AjaxResult.error("未登录");
    }

    @RequestMapping("/history/delete")
    @ResponseBody
    public AjaxResult historyDelete(@RequestBody TeaSearch teaSearch) {
        /*TeaUser tmp = new TeaUser();
        tmp.setOpenId(teaSearch.getOpenId());
        List<TeaUser> teaUserList = iTeaUserService.selectTeaUserList(tmp);
        if (teaUserList != null && teaUserList.size() != 0) {
            teaSearch.setUserCode(teaUserList.get(0).getUserCode());
            iTeaSearchService.insertTeaSearch(teaSearch);
            return AjaxResult.success();
        }*/
        return AjaxResult.success("删除");
    }

}
